#include "ScnsIli9341.c.h"

#if defined(SCNS_ILI9341_ENABLE)&&SCNS_ILI9341_ENABLE==1

#include "ScnsSmctp.h"
#include "ScnsTimer.h"
#include "spi.h"

const ScnsBspIli9341Config scnsBspIli9341Config[SCNS_ILI9341_MAX]={
    {
        .dir=0,
        .horizon=240,
        .vertical=320,
        .horizonBias=0,
        .verticalBias=0,
        .bgr=0,
        .reverseColor=0,
    },
};

ScnsStatus scnsBspIli9341SetCs(ScnsIli9341Enum stN,uint8 cs)
{
    if(cs==0)
    {
        spi4Block();
        hspi4.Instance->CFG1=(hspi4.Instance->CFG1)&(~0X70000000UL)|(SPI_BAUDRATEPRESCALER_4);
        LCD_CS2_GPIO_Port->BSRR=(LCD_CS2_Pin<<16);
    }
    else
    {
        LCD_CS2_GPIO_Port->BSRR=(LCD_CS2_Pin);
    }
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9341SetBackgroundLight(ScnsIli9341Enum stN,ScnsLcdBackgroundLightStatus status)
{
    LCD_BL_GPIO_Port->BSRR=((status==SCNS_LCD_BACKGROUND_LIGHT_ON?0:1))?(LCD_BL_Pin):(LCD_BL_Pin<<16);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9341WReg(ScnsIli9341Enum stN,uint8 dat)
{
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin<<16);
    HAL_SPI_Transmit(&hspi4,&dat,1,500);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9341WData(ScnsIli9341Enum stN,uint8 dat)
{
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin);
    HAL_SPI_Transmit(&hspi4,&dat,1,500);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9341WData16S(ScnsIli9341Enum stN,uint16 dat[],uint32 len)
{
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin);
    HAL_SPI_Transmit(&hspi4,(uint8*)dat,sizeof(uint16)*len,500);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9341WData16SAsync(ScnsIli9341Enum stN,uint16 dat[],uint32 len)
{
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin);
    HAL_SPI_Transmit_DMA(&hspi4,(uint8*)dat,sizeof(uint16)*len);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9341RData(ScnsIli9341Enum stN,uint8 dat[],uint32 len)
{
    hspi4.Instance->CFG1=(hspi4.Instance->CFG1)&(~0X70000000UL)|(SPI_BAUDRATEPRESCALER_16);
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin);
    HAL_SPI_Receive(&hspi4,dat,len,500);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspIli9341RDataAsync(ScnsIli9341Enum stN,uint8 dat[],uint32 len)
{
    hspi4.Instance->CFG1=(hspi4.Instance->CFG1)&(~0X70000000UL)|(SPI_BAUDRATEPRESCALER_16);
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin);
    HAL_SPI_Receive_DMA(&hspi4,dat,len);
    return SCNS_STATUS_OK;
}

#endif
